package com.shenglaotuan.community.common.utils;

import com.shenglaotuan.community.common.entity.JwtConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * Token相关的工具类
 */
@Component
public class TokenUtils {

    private static String tokenHeader;

    /**
     * 根据Request获取id
     * @param request
     * @param jwtConfig
     * @return id
     */
    public static String getIdByRequest(HttpServletRequest request, JwtConfig jwtConfig){
        String token = request.getHeader(tokenHeader);
        String id = jwtConfig.getTokenClaim(token).getSubject();
        return id;
    }

    @Value("${config.jwt.header}")
    public void setTokenHeader(String tokenHeader) {
        TokenUtils.tokenHeader = tokenHeader;
    }

}
